package com.lm.flight.example;

import org.apache.arrow.flight.*;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.VectorSchemaRoot;

public class FlightClientExample {

    public static void main(String[] args) {
        try (BufferAllocator allocator = new RootAllocator();
             FlightClient client = FlightClient.builder()
                     .allocator(allocator)
                     .location(Location.forGrpcInsecure("localhost", 8815))
                     .build()) {

            System.out.println("🚀 Connecting to Arrow Flight server...");

            // 发送请求（ticket可用于筛选不同数据流）
            Ticket ticket = new Ticket("test".getBytes());
            try (FlightStream stream = client.getStream(ticket)) {
                VectorSchemaRoot root = stream.getRoot();
                while (stream.next()) {
                    for (int i = 0; i < root.getRowCount(); i++) {
                        int id = (int) root.getVector("id").getObject(i);
                        String name = root.getVector("name").getObject(i).toString();
                        System.out.printf("Row %d: id=%d, name=%s%n", i, id, name);
                    }
                }
            }

            System.out.println("✅ Finished reading Arrow Flight stream.");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}